package com.ktgj.hotel.task;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.hotel.common.ElongUtils;
import com.ktgj.hotel.domain.*;
import com.ktgj.hotel.domain.base.HotelDetailInput;
import com.ktgj.hotel.domain.base.HotelDetailOutput;
import com.ktgj.hotel.domain.base.HotelDetailResponse;
import com.ktgj.hotel.domain.common.Hotels;
import com.ktgj.hotel.domain.elong.Hotel;
import com.ktgj.hotel.domain.elong.OrderIdCondition;
import com.ktgj.hotel.domain.rooms.HotelIncrDataRequest;
import com.ktgj.hotel.domain.rooms.HotelIncrDataResponse;
import com.ktgj.hotel.domain.rooms.OrderDetailResult;
import com.ktgj.hotel.domain.rooms.OrderId;
import com.ktgj.hotel.mapper.*;
import com.ktgj.hotel.service.IHotelService;
import com.ktgj.hotel.service.IHotelSupplierLinkService;
import com.ktgj.hotel.service.impl.HotelSupplierLinkServiceImpl;
import org.bouncycastle.cms.PasswordRecipientId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Component
public class HotelScheduler {

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private HotelDifferencesMapper hotelDifferencesMapper;

    /**
     * 酒店订单状态
     * */
    @Scheduled(fixedDelay = 2000 * 60)
    public void HotelOrderStatus()
    {
        OrderIdCondition orderIdCondition = new OrderIdCondition();
        List<OrderId> orderIds = hotelOrderMapper.selectByOrderId();
        if (orderIds != null && orderIds.size() > 0)
        {
            for (OrderId orderId : orderIds)
            {
                HotelOrder hotelOrder = new HotelOrder();
                if (orderId != null)
                {
                    if (orderId.getELongOrderId() != null && orderId.getELongOrderId() != 0)
                    {
                        orderIdCondition.setOrderId(orderId.getELongOrderId());
                        orderIdCondition.setAffiliateConfirmationId(orderId.getOrderNumber());
                        OrderDetailResult orderDetailResult = ElongUtils.orderDetail(orderIdCondition);
                        hotelOrder.setOrderid(orderId.getELongOrderId());
                        /**
                         * 艺龙订单状态枚举
                         *
                         * A-已确认
                         * B-NO SHOW
                         * B1-有预定未查到
                         * B2-待查
                         * B3-暂不确定
                         * C-已结帐
                         * D-删除
                         * E-取消
                         * F-已入住
                         * G-变价
                         * H-变更
                         * N-新单
                         * O-满房
                         * S-特殊
                         * U-特殊满房
                         * V-已审
                         * Z-删除,另换酒店
                         * */
                        if (orderDetailResult.Result.Status == "A")
                        {
                            hotelOrder.setOrderstatusid(100l);
                        }
                        if (orderDetailResult.Result.Status == "B")
                        {
                            hotelOrder.setOrderstatusid(760l);
                        }
                        if (orderDetailResult.Result.Status != "A" && orderDetailResult.Result.Status != "B")
                        {
                            hotelOrder.setOrderstatusid(400l);
                        }
                        hotelOrder.setShowstatus(orderDetailResult.Result.ShowStatus);
                        hotelOrder.setStatus(orderDetailResult.Result.Status);
                        hotelOrderMapper.updateHotelOrder(hotelOrder);
                    }
                }
            }
        }
    }

    /**
     * 酒店差异
     * */
    @Scheduled(cron = "0 0 23 L * ?")
    public void hotelOrderDifferences()
    {
        OrderIdCondition orderIdCondition = new OrderIdCondition();
        List<OrderId> orderIds = hotelOrderMapper.selectByOrderDifferences();
        if (orderIds != null && orderIds.size() > 0)
        {
            for (OrderId orderId : orderIds)
            {
                orderIdCondition.setOrderId(0L);
                orderIdCondition.setAffiliateConfirmationId(orderId.getELongOrderId().toString());
                OrderDetailResult orderDetailResult = ElongUtils.orderDetail(orderIdCondition);
                HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderById(orderId.getELongOrderId());
                if (hotelOrder.getShowstatus() != orderDetailResult.Result.ShowStatus && !hotelOrder.getStatus().equals(orderDetailResult.Result.Status))
                {
                    Long i = hotelDifferencesMapper.queryByOrderId(hotelOrder.getOrderid());
                    if (i == null)
                    {
                        HotelDifferences hotelDifferences = new HotelDifferences();
                        hotelDifferences.setOrderid(hotelOrder.getOrderid());
                        hotelDifferences.setSupplierorderid(hotelOrder.getSupplierorderid());
                        hotelDifferences.setHotelid(hotelOrder.getHotelid());
                        hotelDifferences.setHotelname(hotelOrder.getHotelname());
                        hotelDifferences.setShowstatus(hotelOrder.getShowstatus());
                        hotelDifferences.setStatus(hotelOrder.getStatus());
                        hotelDifferences.setOrdertype(hotelOrder.getOrdertype());
                        hotelDifferences.setCreateTime(hotelOrder.getCreateTime());
                        hotelDifferencesMapper.insertHotelDifferences(hotelDifferences);
                    }
                }
            }
        }
    }

    /**
     * 酒店增量
     * */
    @Scheduled(cron = "0 0 1 * * ?")
    public void updateHotel(){
//        int count = 1000;
//        Long lastId = 1l;
//        List<Long> longList = new ArrayList<>();
//        HotelIncrDataResponse hotelIncrDataResponse;
//        do {
//            HotelIncrDataRequest request = new HotelIncrDataRequest();
//            request.setLastId(lastId);
//            request.setCount(count);
//            hotelIncrDataResponse = hotelSupplierLinkService.hotelIncrData(request);
//            if (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0){
//                for (HotelIncrDataResponse.HotelResult.Hotel hotel : hotelIncrDataResponse.getResult().getHotels()) {
//                    longList.add(hotel.getLastId());
//                }
//                longList.sort((x, y) -> Double.compare(y, x));
//                lastId = longList.get(0);
//            }
//        } while (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0);
    }

    /**
     * 取消酒店订单
     */
    @Scheduled(fixedDelay = 1000 * 60)
    public void cancelTrainOrder() {
        List<HotelOrder> hotelOrders = hotelOrderMapper.hotelOrderList();
        for (HotelOrder hotelOrder : hotelOrders) {
            hotelOrder.setOrderstatusid(800l);
            hotelOrderMapper.updateHotelOrder(hotelOrder);
        }
    }
}
